package com.work.straighttalk.model;

import android.media.AudioManager;
import android.media.RingtoneManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import com.hyphenate.chat.EMCallStateChangeListener;
import com.hyphenate.chat.EMClient;
import com.work.straighttalk.R;
import com.work.straighttalk.model.i.OnVoiceCallListener;
import com.work.straighttalk.model.i.VoiceCallMI;

/**
 * Created by dllo on 16/3/11.
 */
public class VoiceCallMImpl implements VoiceCallMI {

    @Override
    public void isCall(String username, boolean isInCommintCall, OnVoiceCallListener listener) {
        if (!isInCommintCall) {//拨打电话
//            soundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
//            comingBtnContainer.setVisibility(View.INVISIBLE);
//            hangupBtn.setVisibility(View.VISIBLE);
//            st1 = "正在连接对方...";
//            callStateTextView.setText(st1);
//            handler.sendEmptyMessage(MSG_CALL_MAKE_VOICE);
            Log.d("VoiceCallMImpl", "拨打电话");
            listener.callPhone();
        } else {//有电话进来
            Log.d("VoiceCallMImpl", "有电话进来");
//            voiceContronlLayout.setVisibility(View.INVISIBLE);
//            Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
//            audioManager.setMode(AudioManager.MODE_RINGTONE);
//            audioManager.setSpeakerphoneOn(true);
//            ringtone = RingtoneManager.getRingtone(this, ringUri);
//            ringtone.play();
            listener.mComingCall();
        }
    }

    @Override
    public void conversation(final OnVoiceCallListener listener) {
        EMClient.getInstance().callManager().addCallStateChangeListener(new EMCallStateChangeListener() {
            @Override
            public void onCallStateChanged(CallState callState, CallError callError) {
                switch (callState) {
                    case CONNECTING://正在连接对方

                                listener.mCinnecting();
//                                callStateTextView.setText(st1);
                        Log.d("VoiceCallMImpl", "正在连接对方");

                        break;
                    case CONNECTED://双方已经建立连接
                        Log.d("VoiceCallMImpl", "双方已经建立连接");
                        listener.mConnected();
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                callStateTextView.setText("已经和对方建立连接，等待对方接受...");
//                            }
//                        });
                        break;
                    case ACCEPTED://电话已经接通
                        listener.mAcceptend();
                        Log.d("VoiceCallMImpl", "电话已经接通");
//                        handler.removeCallbacks(timeoutHangup);
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                try {
//                                    if (soundPool != null)
//                                        soundPool.stop(streamID);
//                                } catch (Exception e) {
//                                }
//                                if (!isHandsfreeState)
//                                    closeSpeakerOn();
//                                //显示是否为直连，方便测试
//                                ((TextView) findViewById(R.id.tv_is_p2p)).setText(EMClient.getInstance().callManager().isDirectCall()
//                                        ? "Direct" : "Relay");
//                                chronometer.setVisibility(View.VISIBLE);
//                                chronometer.setBase(SystemClock.elapsedRealtime());
//                                // 开始记时
//                                chronometer.start();
//                                String str4 = "通话中...";
//                                callStateTextView.setText(str4);
//                                callingState = CallingState.NORMAL;
//                            }
//                        });
                        break;
                    case DISCONNNECTED://电话断了
                        Log.d("VoiceCallMImpl", "电话断了");
                        listener.mDisconnnected(callError);
//                        handler.removeCallbacks(timeoutHangup);
//                        final CallError fError = callError;
//                        runOnUiThread(new Runnable() {
//                            private void postDelayedCloseMsg() {
//                                handler.postDelayed(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        runOnUiThread(new Runnable() {
//                                            @Override
//                                            public void run() {
//                                                Log.d("AAA", "CALL DISCONNETED");
//                                                Animation animation = new AlphaAnimation(1.0f, 0.0f);
//                                                animation.setDuration(800);
//                                                findViewById(R.id.root_layout).startAnimation(animation);
//                                                finish();
//                                            }
//                                        });
//                                    }
//                                }, 200);
//                            }
//
//                            @Override
//                            public void run() {
//
//                            }
//                        });
                        break;
                }
            }
        });


    }
}
